package com.ciwong.epaper.modules.epaper.ui;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;

import com.ciwong.epaper.R;
import com.ciwong.epaper.modules.epaper.adapter.SelectQuestionAdapter;
import com.ciwong.epaper.modules.epaper.bean.SwitchQuestionBean;
import com.ciwong.epaper.modules.epaper.util.ListenSpeakUtil;
import com.ciwong.epaper.util.IntentFlag;
import com.ciwong.mobilelib.ui.BaseActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * 听说模考选题界面
 *
 * @author sugy
 * @version ciwong v.1.0 2016/2/25 12:05
 * @since ciwong v.1.0
 */
public class SelectQuestionActivity extends BaseActivity
        implements ListenSpeakUtil.IRefreshDoRecord
{
    private ListView mLv;

    private Button submitBtn;

    private SelectQuestionAdapter adapter;

    private List<Boolean>            isDoRecords;
    private List<SwitchQuestionBean> mSwitchQuestionList = new ArrayList<>();

    @Override
    protected int setView()
    {
        return R.layout.select_question;
    }

    @Override
    protected void preCreate()
    {
        isDoRecords = (List<Boolean>) getIntent()
                .getSerializableExtra(IntentFlag.INTENT_FLAG_OBJ_LIST);
    }

    @Override
    protected void findViews()
    {
        mLv = (ListView) findViewById(R.id.select_ques_lv);
        submitBtn = (Button) findViewById(R.id.submit_btn_look_result);
    }

    @Override
    protected void initEvent()
    {
        setBackText("关闭");
        setTitleText(R.string.please_select_question);



        mLv.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id)
            {

                SwitchQuestionBean switchQuestionBean = mSwitchQuestionList.get(position);
                if(switchQuestionBean!=null&&!switchQuestionBean.isButton()){
                    boolean isDone = switchQuestionBean.isComplected();
                    if (!isDone)
                    {
                        Intent intent = new Intent();
                        intent.putExtra(IntentFlag.INTENT_FLAG_ID, position);
                        setResult(RESULT_OK, intent);
                        finish();
                    }
                }

            }
        });
    }

    @Override
    protected void init()
    {
        if(isDoRecords!=null&&isDoRecords.size()>0){
            for (Boolean isDoRecord : isDoRecords) {
                SwitchQuestionBean switchQuestionBean = new SwitchQuestionBean();
                switchQuestionBean.setButton(false);
                switchQuestionBean.setComplected(isDoRecord);
                mSwitchQuestionList.add(switchQuestionBean);
            }
            adapter = new SelectQuestionAdapter(this, mSwitchQuestionList);
            mLv.setAdapter(adapter);
        }
    }

    @Override
    protected void onDestroy()
    {
        super.onDestroy();
    }

    /**
     * 刷新是否做的记录
     * 
     * @param isDoRecords
     */
    @Override
    public void refreshDoRecord(List<Boolean> isDoRecords)
    {
        this.isDoRecords = isDoRecords;
        for (int i = 0; i < isDoRecords.size(); i++) {
            Boolean aBoolean = isDoRecords.get(i);
            SwitchQuestionBean switchQuestionBean = mSwitchQuestionList.get(i);
            switchQuestionBean.setComplected(aBoolean);
        }
        adapter.notifyDataSetChanged();
    }

    @Override
    protected void loadData()
    {

    }

}
